package com.jiangxjsy.crm.support;

import cn.hutool.core.date.DateUtil;
import com.jiangxjsy.core.common.CrmModule;
import com.jiangxjsy.core.enums.CrmBackLogEnum;
import com.jiangxjsy.core.enums.FieldSearchEnum;
import com.jiangxjsy.core.exception.BasicException;
import com.jiangxjsy.core.wrap.ResultCode;
import com.jiangxjsy.crm.client.AdminClient;
import com.jiangxjsy.crm.service.CrmBackLogDealService;
import com.jiangxjsy.model.crm.bo.CrmBackLogBO;
import com.jiangxjsy.model.crm.bo.CrmSearchBO;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/20 17:21
 */

@Component
public class CrmBackLogDealSupport {
    @Resource
    private CrmBackLogDealService crmBackLogDealService;

    @Resource
    private AdminClient adminClient;

    /**
     * 设置查询条件
     *
     * @param crmBackLogBO 查询条件
     * @param crmEnum 模块枚举
     * @param userId 用户ID
     */
    public void setCrmBackLogBO(CrmBackLogBO crmBackLogBO, CrmModule crmEnum, Long userId) {
        Integer type = crmBackLogBO.getType();
        Integer isSub = crmBackLogBO.getIsSub();
        if (type == 1) {
            CrmSearchBO.Search search = new CrmSearchBO.Search("nextTime", "datetime", FieldSearchEnum.RANGE);
            search.setValues(Collections.singletonList("today"));
            crmBackLogBO.getData().add(search);
            crmBackLogBO.getData().add(new CrmSearchBO.Search("lastTime", "datetime", FieldSearchEnum.SCRIPT).setScript(new Script(ScriptType.INLINE, "painless", "if (doc['lastTime'].size()==0) {return false} else {return doc['lastTime'].value.toInstant().toEpochMilli() < doc['nextTime'].value.toInstant().toEpochMilli()}", new HashMap<>())));
        } else if (type == 2) {
            crmBackLogBO.getData().add(new CrmSearchBO.Search("nextTime", "datetime", FieldSearchEnum.LT, Collections.singletonList(DateUtil.date().toString())));
            crmBackLogBO.getData().add(new CrmSearchBO.Search("lastTime", "datetime", FieldSearchEnum.SCRIPT).setScript(new Script(ScriptType.INLINE, "painless", "if (doc['lastTime'].size()==0) {return false} else {return doc['lastTime'].value.toInstant().toEpochMilli() < doc['nextTime'].value.toInstant().toEpochMilli()}", new HashMap<>())));
        } else if (type == 3) {
            CrmSearchBO.Search search = new CrmSearchBO.Search("lastTime", "datetime", FieldSearchEnum.RANGE);
            search.setValues(Collections.singletonList("today"));
            crmBackLogBO.getData().add(search);
            crmBackLogBO.getData().add(new CrmSearchBO.Search("lastTime", "datetime", FieldSearchEnum.SCRIPT).setScript(new Script(ScriptType.INLINE, "painless", "if (doc['lastTime'].size()==0) {return false} else {return doc['lastTime'].value.toInstant().toEpochMilli() <= doc['nextTime'].value.toInstant().toEpochMilli()}", new HashMap<>())));
        } else {
            throw new BasicException(ResultCode.SystemResultCode.SYSTEM_NO_VALID);
        }
        if (isSub == 1) {
            crmBackLogBO.getData().add(new CrmSearchBO.Search("ownerUserId", "text", FieldSearchEnum.IS, Collections.singletonList(userId.toString())));
        } else if (isSub == 2) {
            List<String> collect = adminClient.queryChildUserId(userId).getData().stream().map(Object::toString).collect(Collectors.toList());
            crmBackLogBO.getData().add(new CrmSearchBO.Search("ownerUserId", "text", FieldSearchEnum.IS, collect));
        } else {
            throw new BasicException(ResultCode.SystemResultCode.SYSTEM_NO_VALID);
        }
        if (type == 1 && isSub == 1) {
            CrmBackLogEnum model = CrmBackLogEnum.TODAY_CUSTOMER;
            if (crmEnum.equals(CrmModule.LEADS)) {
                model = CrmBackLogEnum.TODAY_LEADS;
            } else if (crmEnum.equals(CrmModule.BUSINESS)) {
                model = CrmBackLogEnum.TODAY_BUSINESS;
            }
            List<String> dealIdList = crmBackLogDealService.queryTypeId(model.getType(), crmEnum.getType(), userId);
            crmBackLogBO.getData().add(new CrmSearchBO.Search("_id", "text", FieldSearchEnum.IS_NOT, dealIdList));
        }
    }

    /**
     * 获取超时查询条件
     *
     * @param crmEnum 模块枚举
     * @param userId 用户ID
     * @return 查询条件
     */
    public Map<String, Object> getOvertimeQueryData(CrmModule crmEnum, Long userId) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("table", crmEnum.getTableName());
        map.put("type", crmEnum.getType());
        map.put("userId", userId);
        map.put("model", CrmBackLogEnum.valueOf("TODAY_"+crmEnum.name()).getType());
        return map;
    }
}
